package com.lyh.test.utils;

import com.lyh.test.SqlParamAlter;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

@Intercepts({
        @Signature(type = Executor.class,method = "query", args = { MappedStatement.class, Object.class,
                RowBounds.class, ResultHandler.class })})
public class CoustomParamAlter implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("要拦截的方法: " + invocation.getMethod());
        //得到当前对象
        Object target = invocation.getTarget();
        System.out.println("拦截的对象：" + target);
        MetaObject metaObject = SystemMetaObject.forObject(target);
        Object[] args = invocation.getArgs();

        /*if (args[0] instanceof MappedStatement) {
            MappedStatement statement = (MappedStatement) args[0];
            System.out.println("=====================statement: " + statement);
            System.out.println("=====================statement id: " + statement.getId());
            System.out.println("=====================statement paraneterMap: " + statement.getParameterMap());
            String id = statement.getId();
            int i = id.lastIndexOf(".");
            String className = id.substring(0, i);
            System.out.println("=====================className: " + className);
            Class<?> clasz = Class.forName(className);

            String methodName = id.substring(i+1);
            System.out.println("=====================methodName: " + methodName);
            List<Method> methods = Arrays.asList(clasz.getMethods());
            Method method = null;
            for (Method m : methods) {
                if(methodName.equals(m.getName())){
                    method = m;
                    break;
                }
            }
            SqlParamAlter sqlParamAlter = method.getAnnotation(SqlParamAlter.class);
            String targetText = sqlParamAlter.target();
            System.out.println(targetText);
            String type = sqlParamAlter.type();
            System.out.println(type);

        }*/

        //执行原来的方法
        Object prObject = invocation.proceed();

        return prObject;
    }

    @Override
    public Object plugin(Object target) {
        return Interceptor.super.plugin(target);
    }

    @Override
    public void setProperties(Properties properties) {
        Interceptor.super.setProperties(properties);
    }
}
